Imports Aricie.DNN.Caching
Imports Microsoft.Msagl.Drawing
Imports QuickGraph

Namespace Aricie.DNN.Caching.Analysis.Graphs
    Public Class CacheGraphCommand
        Inherits MsaglLayoutCommand(Of DistributedCachingNode, DistributedCachingEdge)

        Private _AutoConfigSettings As AutoConfigSettings

        Public Sub New()

        End Sub

        Public Sub New(ByVal autoCOnfig As AutoConfigSettings)
            Me._AutoConfigSettings = autoCOnfig
        End Sub

        Public Shared Function FormatText(ByVal key As String, ByVal maxLength As Integer) As String
            If (key.Length <= 25) OrElse (maxLength <= 25) Then
                Return key.Substring(0, Math.Min(key.Length, maxLength))
            End If
            Dim toReturn As New StringBuilder
            Dim i As Integer = 0
            While i < key.Length - 25
                toReturn.Append(key.Substring(i, 25))
                toReturn.Append(vbCrLf)
                i += 25
            End While
            If i < key.Length Then
                toReturn.Append(key.Substring(i))
            End If

            Return toReturn.ToString
        End Function

        Public Overrides Sub PrepareNode(ByVal vertex As DistributedCachingNode, ByRef objNode As Node)
            objNode.LabelText = FormatText(vertex.ToString, Integer.MaxValue) & vbCrLf

            Select Case vertex.NodeType
                Case TargetNodeType.KeyStat
                    'objNode.Attr.Id = CacheKeyStatistics.FormatKey(vertex.Key)
                    Select Case vertex.KeyStat.Diagnostic.SortedDistributionTypes.Count
                        Case 0
                            objNode.Attr.FillColor = Color.LightGray
                        Case Else
                            Select Case vertex.KeyStat.Diagnostic.SortedDistributionTypes(0).DistributionType
                                Case CacheDistributionType.NotDistributed
                                    objNode.Attr.Shape = Shape.Box
                                    objNode.Attr.FillColor = Color.LightBlue
                                Case CacheDistributionType.Serialized
                                    objNode.Attr.Shape = Shape.Octagon
                                    objNode.Attr.FillColor = Color.YellowGreen
                                Case CacheDistributionType.XmlSerialized
                                    objNode.Attr.Shape = Shape.InvHouse
                                    objNode.Attr.FillColor = Color.Yellow
                                Case CacheDistributionType.JsonNet
                                    objNode.Attr.Shape = Shape.Triangle
                                    objNode.Attr.FillColor = Color.LightPink
                                Case CacheDistributionType.NativeBinary
                                    objNode.Attr.Shape = Shape.House
                                    objNode.Attr.FillColor = Color.LightGreen
                                Case CacheDistributionType.AsString
                                    objNode.Attr.Shape = Shape.Circle
                                    objNode.Attr.FillColor = Color.OliveDrab
                            End Select
                            'if  compressed
                            objNode.Attr.Color = Color.Red

                            objNode.LabelText &= vbCrLf
                            objNode.LabelText &= "Mode: " & vertex.KeyStat.Diagnostic.SortedDistributionTypes(0).DistributionType.ToString & vbCrLf
                            objNode.LabelText &= "Average Duration: " & vertex.KeyStat.Diagnostic.SortedDistributionTypes(0).MeanTime.ToString & vbCrLf
                            If vertex.KeyStat.Diagnostic.SortedDistributionTypes.Count = 1 Then
                                objNode.Attr.FillColor = Color.Orange
                            Else

                                objNode.LabelText &= "Second Mode Ratio: " & vertex.KeyStat.Diagnostic.SortedDistributionTypes(1).RatioToBest & "%"
                            End If
                    End Select

                    'objNode.Label.FontSize = 40

                    'objNode.Attr.Shape = Shape.Ellipse
                Case TargetNodeType.Group
                    objNode.LabelText &= vbCrLf
                    For Each key As DistributedCachingNode In vertex.KeysGroup.Keys
                        objNode.LabelText &= key.ToString & vbCrLf
                    Next
                    objNode.Attr.Shape = Shape.Diamond
                    objNode.Attr.FillColor = Color.Green

            End Select


        End Sub

        Public Overrides Sub PrepareEdge(ByVal edge As DistributedCachingEdge, ByRef objEdge As Microsoft.Msagl.Drawing.Edge)
            MyBase.PrepareEdge(edge, objEdge)

            objEdge.LabelText = edge.Tag.Count & "(" & edge.Tag.Frequency & "%)"

            If edge.Tag.IsMain Then
                objEdge.Attr.Color = Color.Salmon
            Else
                objEdge.Attr.Color = Color.LightGray
            End If

            objEdge.Attr.LineWidth = Math.Min(edge.Tag.Count, Convert.ToInt32(10 * edge.Tag.Frequency / 100))
        End Sub

    End Class
End Namespace